package com.ruoyi.merchant.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.sms.aliyun.AliSmsTemplate;
import com.ruoyi.common.sms.aliyun.SmsResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.mapper.MerchantUserMapper;
import com.ruoyi.merchant.domain.MerchantUser;
import com.ruoyi.merchant.service.IMerchantUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 商家信息Service业务层处理
 * 
 * @author dongdong
 * @date 2025-10-21
 */
@Service
public class MerchantUserServiceImpl implements IMerchantUserService {

    @Autowired
    private AliSmsTemplate smsUtils;

    @Resource
    private MerchantUserMapper merchantUserMapper;

    /**
     * 查询商家信息
     * 
     * @param id 商家信息主键
     * @return 商家信息
     */
    @Override
    public MerchantUser selectMerchantUserById(Long id)
    {
        return merchantUserMapper.selectMerchantUserById(id);
    }

    /**
     * 查询商家信息列表
     * 
     * @param merchantUser 商家信息
     * @return 商家信息
     */
    @Override
    public List<MerchantUser> selectMerchantUserList(MerchantUser merchantUser)
    {
        return merchantUserMapper.selectMerchantUserList(merchantUser);
    }

    /**
     * 新增商家信息
     * 
     * @param merchantUser 商家信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMerchantUser(MerchantUser merchantUser) {
        MerchantUser mu = new MerchantUser();
        mu.setUsername(merchantUser.getUsername());
        List<MerchantUser> muList = merchantUserMapper.selectMerchantUserList(mu);
        if (muList.size() > 0) {
            throw new RuntimeException("登录账号已存在");
        }
        String password = PasswordUtil.generateDefaultPassword();
        merchantUser.setPassword(password);
        merchantUser.setCreateTime(DateUtils.getNowDate());
        merchantUser.setCreateBy(getUsername());
        int res = merchantUserMapper.insertMerchantUser(merchantUser);
        if (res > 0) {
            Map<String, String> params = new HashMap<>();
            params.put("username", merchantUser.getUsername());
            params.put("password", password);
            SmsResult smsResult = smsUtils.sendSms(merchantUser.getPhone(), "SMS_491140027", params);
            if (!smsResult.isSuccess()) {
                throw new RuntimeException("商户注册短信发送失败");
            }
        }
        return res;
    }

    /**
     * 修改商家信息
     * 
     * @param merchantUser 商家信息
     * @return 结果
     */
    @Override
    public int updateMerchantUser(MerchantUser merchantUser)
    {
        merchantUser.setUpdateTime(DateUtils.getNowDate());
        return merchantUserMapper.updateMerchantUser(merchantUser);
    }

    /**
     * 批量删除商家信息
     * 
     * @param ids 需要删除的商家信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantUserByIds(Long[] ids)
    {
        return merchantUserMapper.deleteMerchantUserByIds(ids);
    }

    /**
     * 删除商家信息信息
     * 
     * @param id 商家信息主键
     * @return 结果
     */
    @Override
    public int deleteMerchantUserById(Long id)
    {
        return merchantUserMapper.deleteMerchantUserById(id);
    }

    @Override
    public List<Map<String, Object>> selectList() {
        return merchantUserMapper.selectList();
    }
}
